package com.lgc.eventsounds;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.MalformedURLException;

public class FileResourceLoader {
  public static InputStream getFileResource(String resFileName)
  {
    InputStream retVal = null;
    try {
      boolean loadedResFileName = false;

      //Check for http URL
      try {
        URL url = new URL(resFileName);
        retVal = url.openStream();
        loadedResFileName = true;
      } catch(MalformedURLException mue) {
        //Fall through to try local file
        retVal = null;
        loadedResFileName = false;
      }
      //Check for local file
      if(!loadedResFileName) {
        try {
          retVal = new FileInputStream(resFileName);
          loadedResFileName = true;
        } catch(FileNotFoundException fnfe) {
          //Fall through to try resource file
          retVal = null;
          loadedResFileName = false;
        }
      }
      //Check for resource file
      if(!loadedResFileName) {
        retVal = FileResourceLoader.class.getResourceAsStream(resFileName);
        loadedResFileName = true;
      }
    } catch(Throwable t) {
      retVal = null;
    }
    return(retVal);
  }
}
